<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>角色管理</title>
    <link rel="shortcut icon" href="favicon.ico">
     <link href="<%=path%>/hplus/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="<%=path%>/hplus/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="<%=path%>/hplus/css/plugins/iCheck/custom.css" rel="stylesheet"> 
    <link href="<%=path%>/hplus/css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/animate.min.css" rel="stylesheet">
    <link href="<%=path%>/hplus/css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.css">
    <link href="${pageContext.request.contextPath}/hplus/css/plugins/zTree/zTreeStyle/metro.css" rel="stylesheet">
    
    <style> 
        .radio label{
            padding-left: 0;
        }

        .date{
            margin: -8px 20px 0 0;
        }

        .clockpicker{
            margin-top: -8px;
        }

        .i-checks label{
            float: left;
            margin-right: 10px;
        }

        .col-sm-offset-1{
            margin-top: 20px;
        }

        .tishi{
            margin-top: 7px;
            display: inline-block;
        }

        .alert{
            position:absolute;
            top: -15px;
            left: 8%;
            display:none;
        }
        .text-center{
            width:50%;
            margin: 10px 0 20px;
        }

        .radio label::before,.checkbox label::before {
            display: none;
        }
        .form-horizontal .radio{
            display: inline-block;
        }
        .aaa{
            position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;
        }
        .s_line{
            height: 16px;
            line-height: 16px;
            width: 1px;
            float: left;
            margin: 8px 20px;
            background: #666;
        }
        .red{
            color: red;
            margin-right: 5px;
        }
        
        .ztree li a.curSelectedNode{
		background-color:#BEEBFF;
		}
		
		.ztree li span.button.noline_close{background-position: -126px -84px;}									
	    .ztree li span.button.noline_open{background-position: -105px -84px;}
	
    </style>
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="ibox-content">
            <form method="get" class="form-horizontal">
                <div class="form-group">
                    <label class="col-sm-1 control-label"><span class="red" style="margin-right:2px">*</span>角色名称</label>
                    <div class="col-sm-3">
                        <input id="roleName" type="text" maxlength="10" class="form-control" placeholder="请勿超过十个字" >
                    </div>
                    <a class="text-navy" style="line-height:34px;" data-toggle="modal" data-target="#myModa2" onclick="copyRole();">复制角色</a>
                </div>                
                

				<!-- 模块权限部分数据填充 -->
                <div class="form-group">
                    <label class="col-sm-1 control-label"><span class="red" style="margin-right:2px">*</span>模块权限</label>
                    <div class="col-sm-11">
					<!-- 引入zTree -->		
					<div style="overflow-x:auto;height:250px;"> 
                    <ul id="tree" class="ztree" ></ul>
                    </div>   
                    </div>
                </div>

				<!-- 外部应用展示模块 -->
                <div class="form-group">
                    <label class="col-sm-1 control-label">外部应用</label>
                    <div class="col-sm-11">                      
                     <div class="col-sm-2" style="display:inline-block;padding-left:0;padding-top:0;" >
	                      <label class="checkbox-inline i-checks" onclick ="selectCrmCheck();">
	                           <div class="icheckbox_square-green" style="position: relative;">			
	                               <input  id="crmCheckId" type="checkbox" value="option1" style="position: absolute; opacity: 0;">
	                               <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"></ins>
	                            </div> CRM	
	                      </label>
	                      <div  style="display:inline-block;padding-top:0;">
	                      <button id="crmSetButton" style="display:none;" data-toggle="modal" data-target="#myModa3" type="button" class="btn btn-primary btn-sm k_verify">设置</button>
	                      </div>
	                 </div>    			
	                 <!-- 默认把门店app选项置为隐藏 -->
	                 <div class="col-sm-10" style="display:inline-block;padding-left:0;padding-top:0;">
	                     <div id="appCheckDiv" style="position:absolute;width:90px;height:35px;left:0;top:0;opacity:0.1;background-color:black;z-index: 9999;"></div>		
	                      <label class="checkbox-inline i-checks">
	                           <div class="icheckbox_square-green" style="position: relative;">			
	                               <input  id="appCheckId" type="checkbox" value="option1" style="position: absolute; opacity: 0;">
	                               <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"></ins>
	                            </div> 门店APP					
	                      </label>			
	                 </div>     
                    </div>		
                </div>
                

				<!-- 数据权限展示模块 -->
                <div class="form-group" hidden= "">
                    <label class="col-sm-1 control-label">数据类型权限</label>
                    <div class="col-sm-11">                      
                     <div class="col-sm-2" style="display:inline-block;padding-left:0;padding-top:0;" >
	                      <label class="checkbox-inline i-checks" >
	                           <div class="icheckbox_square-green" style="position: relative;">			
	                               <input  id="storeDataAuth" type="checkbox" checked="checked" value="option1" style="position: absolute; opacity: 0;">
	                               <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"></ins>
	                            </div> 门店数据
	                      </label>
	                 </div>    			
	                 <div class="col-sm-10" style="display:inline-block;padding-left:0;padding-top:0;">
	                      <label class="checkbox-inline i-checks">
	                           <div class="icheckbox_square-green" style="position: relative;">			
	                               <input  id="departeDataAuth" type="checkbox" checked="checked" value="option1" style="position: absolute; opacity: 0;">
	                               <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"></ins>
	                            </div> 部门数据				
	                      </label>			
	                 </div>     
                    </div>		
                </div>

                <div class="form-group">
                    <label class="col-sm-1 control-label">角色描述</label>
                    <div class="col-sm-5">                      
                        <textarea id="ccomment" name="comment" maxlength="30" class="form-control" required="" aria-required="true"></textarea>                       
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-2 col-sm-offset-1">
                        <button type="button" class="btn btn-white" data-dismiss="modal" onclick="javascript:window.location.href='<%=path%>/view/ui/roleList';">取消</button>
                        <button onclick="javascript:save();" type="button" class="btn btn-primary" style="margin-left:20px;">确定</button>
                    </div>      
                </div>          
            </form>
        </div>    
    </div>

    <!-- 复制角色 -->
    <div hidden="" class="modal inmodal fade" id="myModa2" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content"  style="width:400px;">
                <div class="modal-header" style="padding: 15px 15px 10px;">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="text-left">复制角色</h4>
                </div>
                <div class="text-center" style="width:100%;height:60px;">                    
                    <form method="get" class="form-horizontal col-sm-12" style="margin:20px 0;">
                        <div class="form-group">
                            <label class="col-sm-5 control-label">请选择复制权限的角色</label>
                            <div class="col-sm-6">
                                <select id="roleList" class="form-control" name="account">
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
                 <div class="modal-footer" style="text-align:center;border-top:none;">
                     <button type="button" class="btn btn-outline btn-primary" data-dismiss="modal">取消</button>
                     <button onclick="javascript:choseRole();" type="button" class="btn btn-primary k_verify">确定</button>
                 </div>
             </div>
        </div>
    </div> 

	<!-- 设置CRM权限 -->
    <div hidden="" class="modal inmodal fade" id="myModa3" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content"  style="width:500px;">
                <div class="modal-header" style="padding: 15px 15px 10px;">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="text-left">CRM设置</h4>
                </div>
                <div class="text-center" style="width:100%;height:60px;">     
                <!-- CRM角色 -->
                <div class="form-group">
                    <label class="col-sm-4 control-label">请设置CRM权限</label>
                    <div class="col-sm-8">
                    
                        <div class="radio i-checks">
                            <label><input id = "crmRole1" type="radio" checked="" value="option1" name="crm"> <i></i> 品牌</label>
                        </div>
                        
                        <div class="radio i-checks">
                            <label><input id = "crmRole2" type="radio" value="option2" name="crm"> <i></i> 店长</label>
                        </div>
                        
                        <div class="radio i-checks">
                            <label><input id = "crmRole3"  type="radio" value="option3" name="crm"> <i></i> 店员</label>
                        </div>

                    </div>
                </div>

                </div>
                 <div class="modal-footer" style="text-align:center;border-top:none;">
                     <button type="button" class="btn btn-outline btn-primary" onclick="cancel();">取消</button>
                     <button type="button" class="btn btn-primary k_verify" data-dismiss="modal">确定</button>
                 </div>
             </div>
        </div>
    </div> 



    <script src="<%=path%>/hplus/js/jquery.min.js?v=2.1.4"></script>
    <script src="<%=path%>/hplus/js/bootstrap.min.js?v=3.3.6"></script>
    <script src="<%=path%>/hplus/js/content.min.js?v=1.0.0"></script>
    <script src="<%=path%>/hplus/js/plugins/chosen/chosen.jquery.js"></script>
    <script src="<%=path%>/hplus/js/plugins/jsKnob/jquery.knob.js"></script>
    <script src="<%=path%>/hplus/js/plugins/jasny/jasny-bootstrap.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="<%=path%>/hplus/js/plugins/prettyfile/bootstrap-prettyfile.js"></script>    
    <script src="<%=path%>/hplus/js/plugins/switchery/switchery.js"></script>
    <script src="<%=path%>/hplus/js/plugins/ionRangeSlider/ion.rangeSlider.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/iCheck/icheck.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/metisMenu/jquery.metisMenu.js"></script>
    <script src="<%=path%>/hplus/js/plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
    <script src="<%=path%>/hplus/js/plugins/clockpicker/clockpicker.js"></script>    
    <script src="<%=path%>/hplus/js/plugins/cropper/cropper.min.js"></script>
    <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
    <script src="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.js"></script>
	<script type="text/javascript" src="<%=path%>/js/marketing/dialog.js"></script>
	<script type="text/javascript" src="<%=path%>/js/marketing/public.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
	<script src="<%=path%>/hplus/js/plugins/zTree/jquery.ztree.all-3.5.min.js"></script>
    <script type="text/javascript">

					
    </script>
    
    <script>
	
       //ajax全局同步、此处功能不需要异步
	   $.ajaxSetup({  
	       async : false  
	   });    
    
       //初始化icheck控件
	   $(document).ready(function(){$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",})});
	
        <!-- 页面自加载方法 -->
        $(function(){
        	//上级页面传过来的角色id
            var roleId = getQueryString("id");
        	//上级传过来用于标记是更新还是新增操作的标识create、edit
            var type = getQueryString("type");
            
            if(StringEmpty(roleId)){
            	//获取角色对应的权限信息，用于回显
            	roleInfo();
            }else{
            	//初始化角色菜单树
            	initAuthorityList();
            }
        });
        
    	//编辑默认选中的crm角色
        var defaultCrmSelect = "";
        //获取该角色对应的全部权限，用于回显(编辑使用)
        function roleInfo(){
        	//初始化角色菜单树，不过滤is_deleted的super客户的权限
        	initAuthorityList();
        	//上级页面传过来的角色id
            var roleId = getQueryString("id");
        	//根据角色id拿到该角色对应的全部权限，用于回显
         	var zTree = $.fn.zTree.getZTreeObj("tree");
         	//外部应用回显	
         	var externalAuthority = "";
 
	        var url="<%=path%>/role/ajaxRoleList";
        	$.post(url,{
    		}, function(data) {
    			var realData = data.data;
    			if (data.status == 200) {
    				for(var i=0;i<realData.length;i++){
    					if(realData[i].id == roleId){		
    						 $('#roleName').val(realData[i].name); 
    						 $('#ccomment').val(realData[i].description);
    						 externalAuthority = realData[i].externalAuthority;
    						 break;
    					}
    				}
    			}
    		});        
	  	    //判断是否选中外部应用，判断是否选用CRM设置中的选项
	  	   if(externalAuthority != '' && externalAuthority != null){
	  		   var arr = externalAuthority.split(",");		
				   //如果只有 一个元素，证明只选中外部应用中的CRM，并回显设置里的选项
				   if(arr.length == 1){				
			          $("#appCheckDiv").hide();
			          $("#appCheckId").iCheck('uncheck');
			          $('#crmCheckId').iCheck('check'); 
		        	  $("#crmSetButton").show();
		        	  
		              $('#crmRole'+arr[0]).iCheck('check');
		              defaultCrmSelect = '#crmRole'+arr[0];
				   //如果有两个元素，证明选中了外部应用中的CRM、门店APP
				   }else if(arr.length == 2){
		        	  $("#appCheckDiv").hide();					
				      $('#appCheckId').iCheck('check'); 
					  $('#crmCheckId').iCheck('check'); 
		        	  $("#crmSetButton").show();
		        	  defaultCrmSelect = '#crmRole'+arr[0];
		        	  $('#crmRole'+arr[0]).iCheck('check');
				   }    				
	  	   }else{
	  		   	     $("#appCheckDiv").show();
				      $('#appCheckId').iCheck('uncheck'); 
					  $('#crmCheckId').iCheck('uncheck'); 
		        	  $("#crmSetButton").hide();
		        	  
		        	  $('#crmRole1').iCheck('check');
	  	   }
	         var url="<%=path%>/role/ajaxSelectRoleAuthority";
	  	   //根据角色id获取该角色对应的一批,is_deleted 不为 0的数据
		       	$.post(url, {
		       		"roleId":roleId
				}, function(data) {
					var realData = data.data;
					if (data.status == 200) {
						//首先取消全选	
						zTree.checkAllNodes(false);
						//根据id进行zTree树的回显
						for(var i=0;i<realData.length;i++){
						   var node = zTree.getNodeByParam("id",realData[i].authorityId);
					       zTree.checkNode(node, true, true);
						}
					}else{		
		        		errorDialog(data.message);
		        	}
				}); 
	        	
        }
        
        //获取角色列表
        function RoleList(){
      	var url="<%=path%>/role/ajaxRoleList";
        	$.post(url, {
    		}, function(data) {
    			if (data.status == 200) {
    				initRoleListHtml(data.data);
    			}
    		}); 
        }
        
        //动态填充复制角色中的select
        function initRoleListHtml(data){
         	var html='';
        	for(var i in data){
        		html+='<option productId= "'+data[i].productId+'" externalAuthority="'+data[i].externalAuthority+'" description="'+data[i].description+'" id="'+data[i].id+'">'+data[i].name+'</option>';
        	}
        	$('#roleList').html(html); 
        }
        
        //初始化角色菜单树
        function initAuthorityList(){
        	//保存所有的菜单节点数据
        	var zNodes =[];
			//从数据库查询该公司最大客户对应的菜单权限
         	var url="<%=path%>/role/ajaxAuthorityGroupList";
        	$.post(url, {
    		}, function(data) {
    			var realData = data.data;
    			if (data.status == 200) {				
    				for(var i=0;i<realData.length;i++){
    					var treeData = {id:realData[i].authorityId, pId:realData[i].authorityParentCode, name:realData[i].authorityName, open:false};
    					zNodes.push(treeData);
    				}
    				zTree(zNodes);
    			}else{		
	        		errorDialog(data.message);
	        	}
    		}); 
        }
        
        //初始化模块权限
        function initAuthorityListHtml(data){

        }
        
        //用户点击了保存按钮，进行新增逻辑判断
        function save(){
        	//拿到角色名称
        	var roleName = $('#roleName').val();
        	//用户选择的模块权限
        	var treeObj = $.fn.zTree.getZTreeObj("tree");
            nodes=treeObj.getCheckedNodes(true);
        	//拿到角色权限
        	var authoritIdList = "";
            for(var i=0;i<nodes.length;i++){  
            	if(i == 0){
            		authoritIdList += nodes[i].id; //获取选中节点的值  
            	}else{
            		authoritIdList += "," + nodes[i].id; //获取选中节点的值  
            	}
            }  
        	//如果角色名称不为空，权限不为空,调用检查名称是否重复的方法
        	if(StringEmpty(roleName)&&StringEmpty(authoritIdList)){
        		checkName();
        	}else if(!StringEmpty(roleName)){
        		promptDialog("请输入角色名称");
        		
        	}else if(!StringEmpty(authoritIdList)){
        		promptDialog("请勾选角色权限");
        	} 
        	
        }
        
        //检查名称AJAX调用
        function checkName(){
        	//上级传过来用于标记是更新还是新增操作的标识create、edit
            var type = getQueryString("type");
        	var roleName = $('#roleName').val();
        	//上级页面传过来的角色id
            var roleId = getQueryString("id");
        	
        	//上级页面传过来的角色id
            //var roleId = getQueryString("id");
        	//用户选择的模块权限
        	var treeObj = $.fn.zTree.getZTreeObj("tree");
            nodes=treeObj.getCheckedNodes(true);
        	var authoritIdList = "";
            for(var i=0;i<nodes.length;i++){  
            	if(i == 0){
            		authoritIdList += nodes[i].id + "&" + nodes[i].name ; //获取选中节点的值  
            	}else{
            		authoritIdList += "," + nodes[i].id + "&" + nodes[i].name ; //获取选中节点的值  
            	}
            }  
        	//用户选择的外部应用权限
        	var externalAuthority = "";
            //获取CRM应用的选中状态、获取门店APP的选中状态
            var crmCheckState = $('#crmCheckId').is(':checked');
            //如果CRM应用的选中状态 为选中状态，那么获取设置里用户选择的CRM角色
            var appCheckId = $('#appCheckId').is(':checked');
           
            if(!crmCheckState){
            	promptDialog("CRM权限必须勾选");
            }else{
            
            //如果crm应用被选中,那么获取设置中radio的选中状态			
            if(crmCheckState){
            	var crmRole1 = $('#crmRole1').is(':checked');
            	var crmRole2 = $('#crmRole2').is(':checked');
            	var crmRole3 = $('#crmRole3').is(':checked');
            	if(crmRole1){
            		externalAuthority = 1;
            	}else if(crmRole2){
            		externalAuthority = 2;
            	}else if(crmRole3){
            		externalAuthority = 3;
            	}
                //如果门店APP的选中状态 为选中状态，那么默认加入店员权限1
                if(appCheckId){
                	var appauth = 1;
                	//如果是品牌管理员或者是店长，那么app权限就是老板端
                	if(externalAuthority == 1 || externalAuthority == 2){
                		appauth = 0
                	}else{
                		appauth = 1;
                	}
                	
                	externalAuthority += ","+appauth;
                }
            }
        	//用户选择的角色描述
        	var description = $('#ccomment').val();
        	
            //判断门店数据类型权限是否被选中
            var storeDataAuth = $('#storeDataAuth').is(':checked');
            //判断部门数据类型权限是否被选中
            var departeDataAuth = $('#departeDataAuth').is(':checked');
            
            //该角色所含有的数据权限(目前默认定死两个、部门和门店)
            var dataAuthRole = "";
            
            //如果选中类门店数据权限
			if(storeDataAuth){
				dataAuthRole += "store,";
			}
            //如果选中了部门数据权限
            if(departeDataAuth){
            	dataAuthRole += "department,";
            }
            dataAuthRole = dataAuthRole.substring(0,dataAuthRole.length -1);

        	//如果是新增，那么发送ajax请求检查角色名称是否重复，规则是否正确
        	if(type == "create"){
        		var url = "<%=path%>/role/ajaxCheckName";
            	$.post(url, {
        			"name":roleName,
        		}, function(data) {
        			if (data.status == 200) {
        				//进行新增角色，角色权限，角色外部应用，角色数据权限操作
        	        	create(roleName,authoritIdList,externalAuthority,description,dataAuthRole);
        			}else{
    	        		errorDialog(data.message);
    	        	}
        		});
        	}else if(type == "edit"){
        		var url = "<%=path%>/role/ajaxCheckName";
            	$.post(url, {
        			"name":roleName,
        			"id":roleId
        		}, function(data) {
        			if (data.status == 200) {
                		//进行编辑角色，角色权限，角色外部应用，角色数据权限操作
                		update(roleName,authoritIdList,externalAuthority,description,dataAuthRole);
        			}else{
    	        		errorDialog(data.message);
    	        	}
        		});
        	}
            }
        }		
        
        //新增角色、角色菜单对应关系AJAX调用
        function create(roleName,authoritIdList,externalAuthority,description,dataAuthRole){
         	var url="<%=path%>/role/create";
        	$.post(url, {
    			"authoritIdList":authoritIdList,
    			"name":roleName,
    			"description":description,
    			"externalAuthority":externalAuthority,
    			"roleAttribute":dataAuthRole
    		}, function(data) {
    			if (data.status == 200) {
                    //SuccessDialogByUrlNow("新增角色成功","",'<%=path%>/view/ui/roleList');
                    var url = '<%=path%>/view/ui/roleList';
                    SuccessDialogByUrl("新增角色成功!","",url);
                    setTimeout(function () { window.location.href=url; }, 1600);
    			}else{
	        		errorDialog(data.message);
	        	}
    		});
        }
        
        
      	//更新角色、角色菜单对应关系AJAX调用
        function update(roleName,authoritIdList,externalAuthority,description,dataAuthRole){
        	//上级页面传过来的角色id
            var roleId = getQueryString("id");

         	var url="<%=path%>/role/update";
        	$.post(url, {
    			"authoritIdList":authoritIdList,
    			"name":roleName,
    			"id":roleId,
    			"description":description,
    			"externalAuthority":externalAuthority,
    			"roleAttribute":dataAuthRole
    		}, function(data) {
    			if (data.status == 200) {
                    //SuccessDialogByUrlNow("编辑成功","",'<%=path%>/view/ui/roleList');
                    var url = '<%=path%>/view/ui/roleList';
                    SuccessDialogByUrl("编辑成功!","",url);
                    setTimeout(function () { window.location.href=url; }, 1600);
    			}else{
	        		errorDialog(data.message);
	        	}
    		});
        }
       
       
       
       /*
       chkboxType: { “Y”: “ps”, “N”: “ps” } 
	   Y 属性定义 checkbox 被勾选后的情况； 
	   N 属性定义 checkbox 取消勾选后的情况； 
	   “p” 表示操作会影响父级节点； 
	   “s” 表示操作会影响子级节点。 
	   */
       //初始化zTree
       function zTree(treeData){
       	//上级传过来用于标记是更新还是新增操作的标识create、edit
           var type = getQueryString("type");
		   //如果是编辑
		   var ygx = "";
		   var ngx = "";
		   if(type != "edit"){
			   ygx = "ps";
			   ngx = "ps";		
		   }
		   
           var zTree;
           var setting = {  
               check: { 
            	   enable: true,				
            	   chkStyle: "checkbox",   //勾选框类型(checkbox 或 radio）
            	   chkboxType: { "Y": ygx, "N": ngx }   //勾选 checkbox 对于父子节点的关联关系
               },						
               view: {
                   showLine: false,					
                   selectedMulti: false
               },
               data: {
                   simpleData: {
                       enable:true,
                       idKey: "id",
                       pIdKey: "pId",
                       rootPId: ""
                   }
               },
               callback: {
                   beforeClick: getCurrentNode,
                   onClick : zTreeOnClick
               }
           };
           
           function getCurrentNode(treeId, treeNode) {
               //curNode = treeNode;    
           }
           
           //点击左侧的菜单树，动态展示右侧
           function zTreeOnClick(){
           	   //此处编写需要完成的业务逻辑代码，实现你想要的 主内容的框架进行页面跳转
 			/*  keyWord = "";
           	    $("#bmName_tz").text(curNode.name);
           	    $("#bmid_tz").text(curNode.id);
           	    $("#bmcurd").show();
			    initUser(); */
           	}

           //初始化树
           var t = $("#tree");
           t = $.fn.zTree.init(t, setting, treeData);
       }
	   
       
       //用户选择复制权限的角色，根据角色id去中间表查询该角色对应的全部菜单,动态勾选树结构的复选框(复制权限使用)
       function choseRole(){
    	 var zTree = $.fn.zTree.getZTreeObj("tree");
    	   //alert($("#roleList option:selected").attr("id"));
    	   //角色名称回显
    	   $('#roleName').val($("#roleList option:selected").text());
    	   
    	   $('#ccomment').val($("#roleList option:selected").attr('description'));
    	   //外部应用回显	
    	   var externalAuthority = $("#roleList option:selected").attr('externalAuthority');
    	   //判断是否选中外部应用，判断是否选用CRM设置中的选项
    	   if(externalAuthority != '' && externalAuthority != null){
    		   var arr = externalAuthority.split(",");		
			   //如果只有 一个元素，证明只选中外部应用中的CRM，并回显设置里的选项
			   if(arr.length == 1){				
		          $("#appCheckDiv").hide();
		          $("#appCheckId").iCheck('uncheck');
		          $('#crmCheckId').iCheck('check'); 
	        	  $("#crmSetButton").show();
	        	  
	              $('#crmRole'+arr[0]).iCheck('check');
			   //如果有两个元素，证明选中了外部应用中的CRM、门店APP
			   }else if(arr.length == 2){
	        	  $("#appCheckDiv").hide();					
			      $('#appCheckId').iCheck('check'); 
				  $('#crmCheckId').iCheck('check'); 
	        	  $("#crmSetButton").show();
	        	  
	        	  $('#crmRole'+arr[0]).iCheck('check');
			   }    				
    	   }else{
    		   	  $("#appCheckDiv").show();
			      $('#appCheckId').iCheck('uncheck'); 
				  $('#crmCheckId').iCheck('uncheck'); 
	        	  $("#crmSetButton").hide();
	        	  
	        	  $('#crmRole1').iCheck('check');
    	   }
           var url="<%=path%>/role/ajaxSelectRoleAuthority";
    	   //根据角色id获取该角色对应的一批,is_deleted 不为 0的数据
	       	$.post(url, {
	       		"roleId":$("#roleList option:selected").attr("id")
			}, function(data) {
				var realData = data.data;
				if (data.status == 200) {
					//首先取消全选	
					zTree.checkAllNodes(false);
					//根据id进行zTree树的回显
					for(var i=0;i<realData.length;i++){
					   var node = zTree.getNodeByParam("id",realData[i].authorityId);
				       zTree.checkNode(node, true, true);
				       $('#myModa2').modal('hide');
					}
				}else{		
	        		errorDialog(data.message);
	        	}
			});  
       }
       
       
       //用户点击了CRM外部应用条件触发
       function selectCrmCheck(){
           if($('#crmCheckId').is(':checked') == false){
        	   $("#appCheckDiv").hide();
        	   $("#crmSetButton").show();
           }else{
        	   $("#appCheckDiv").show(); 
        	   $("#crmSetButton").hide();
        	   $("#appCheckId").iCheck('uncheck');
           }		
        }

       //用户取消了CRM权限设置
       function cancel(){
       	   //上级传过来用于标记是更新还是新增操作的标识create、edit
           var type = getQueryString("type");
    	   //选中品牌管理员权限
    	   if(type == "create"){
    	   $('#crmRole1').iCheck('check');
    	   }else{
    	   $(defaultCrmSelect).iCheck('check');
    	   }
    	   //关闭model
    	   $("#myModa3").modal('hide');
       }
       
       
       //复制角色初始化下拉菜单，只有在用户点击复制角色的时候，才触发，提升响应效率
       function copyRole(){
           //初始化复制角色弹框中的下拉菜单
           RoleList();
       }
       
        
    </script>
    
</body>
</html>

